﻿using System;
using System.Data;
using System.Web.UI;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Web.UI;

namespace VitCode.Web.BackStageManagement
{
    public partial class UserImport : VcPage
    {
        CastleAccess acc = new CastleAccess();
        private string sql = "SELECT a.*,b.Name UserGroup  FROM  VitCode_Castle_User a  left outer join VitCode_Castle_UserGroup b on a.UserGroupId =b.Id";

        private string QueryCondition
        {
            get
            {

                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
            if (!IsPostBack)
            {
                if (String.IsNullOrEmpty(GenericContext.CurrUserCode))
                {
                    Response.Redirect("../Default.aspx");
                }
                if (Request.QueryString["id"] != null)
                {
                    labelUserGroupId.Text = publ.GetUrlToReceive(Request.QueryString["id"].Trim());
                }
                ListGridInfo.BindGrid();
            }

        }
 
        public object BindList()
        {
            sql = sql + QueryCondition;
            DataTable dt = acc.GetDataTableBySql(sql);
            return dt;
        }

        protected void btnQuery_Click1(object sender, EventArgs e)
        {

            if (ddlCondition.SelectedItem.Text != "" && txtValue.Text.Trim().Length > 0)
            {
                
                if (rbtAmount.Checked)
                     QueryCondition = " where " + ddlCondition.SelectedItem.Value + " = '" + txtValue.Text.Trim() + "'";
                else
                    QueryCondition = " where " + ddlCondition.SelectedItem.Value + " like '%" + txtValue.Text.Trim() + "%'";
           }
            else
            {
                QueryCondition = "";
            }
            ListGridInfo.BindGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserGroupList.aspx", false);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strUserVal = ListGridInfo.GetSelectedItem(1);
            if(strUserVal.Trim().Length == 0)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "102", "<script>alert('請選擇用戶!')</script>");
                return;
            }
            var strUserId = strUserVal.Split(',');
            int access = 0;

        
            for (int i = 0; i < strUserId.Length; i++)
            {
                if (!string.IsNullOrEmpty(strUserId[i]))
                {
                   string  strUpdateSql = "update VitCode_Castle_User set UserGroupId='" + labelUserGroupId.Text.Trim() + "' where id='" + strUserId[i].Trim() + "'";

                    access = acc.ExecuteCmd(strUpdateSql);
                }
            }
         
            if (access >= 1)
            {
                
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "007", "<script>alert('設置成功!')</script>");
                ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindList);
                 ListGridInfo.BindGrid();
            }
        }
    }
}
